﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AntdUI;
using OptiKinetiq_Platform.ContentDisplay;
using OptikinetiqControls;
using WeifenLuo.WinFormsUI.Docking;

namespace OptiKinetiq_Platform
{
    public partial class MainForm : BorderlessForm
    {
        private static MainForm mainForm;

        private FlowDIsplay flowDisplay;
        private MotionDisplay motionDisplay;
        private DisplayWindow haumWindow;

        private MainForm()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
            InitTheme(new VS2015LightTheme());
            DefaultLayout();
        }

        public static MainForm GetInstance()
        {
            if (mainForm == null || mainForm.IsDisposed)
            {
                mainForm = new MainForm();
            }
            return mainForm;

        }

        private void panel_title_MouseDown(object sender, MouseEventArgs e)
        {
            this.DraggableMouseDown();
        }

        private void button_min_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void button_max_Click(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                this.WindowState = FormWindowState.Maximized;
            }
            else
            {
                this.WindowState = FormWindowState.Normal;
            }
        }

        private void button_close_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void InitTheme(ThemeBase themeBase)
        {
            dockPanel_main.Theme = themeBase;
        }

        private void DefaultLayout()
        {
            
            if (motionDisplay == null || motionDisplay.IsDisposed)
            {
                motionDisplay = new MotionDisplay();
                motionDisplay.Text = "控制窗口";
            }
            motionDisplay.Show(dockPanel_main, DockState.DockBottom);

            if (flowDisplay == null || flowDisplay.IsDisposed)
            {
                flowDisplay = new FlowDIsplay();
                flowDisplay.Text = "可视化";
            }
            flowDisplay.Show(dockPanel_main, DockState.DockRight);

            if (haumWindow == null || haumWindow.IsDisposed) {
                haumWindow = new DisplayWindow();
                haumWindow.Text = "Haum显示窗口";
            }
            haumWindow.Show(dockPanel_main, DockState.Document);
        }


        private void toolStripMenuItem_defaultLayout_Click(object sender, EventArgs e)
        {
            DefaultLayout();
        }

        public void ShowDisplayImage(Display sender)
        {
            haumWindow.ShowImage(sender.image);
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            if (flowDisplay == null || flowDisplay.IsDisposed)
            {
                flowDisplay = new FlowDIsplay();
                flowDisplay.Text = "可视化";
            }
            flowDisplay.Show(dockPanel_main, DockState.DockLeftAutoHide);
        }

        private void 显示窗口ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (haumWindow == null || haumWindow.IsDisposed)
            {
                haumWindow = new DisplayWindow();
                haumWindow.Text = "Haum显示窗口";
            }
            haumWindow.Show(dockPanel_main, DockState.Document);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    flowDisplay.AddDisplay(new Bitmap(ofd.FileName), "图像0");
                }
            }
        }

        private void 控制窗口ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (motionDisplay == null || motionDisplay.IsDisposed)
            {
                motionDisplay = new MotionDisplay();
                motionDisplay.Text = "控制窗口";
            }
            motionDisplay.Show(dockPanel_main, DockState.DockBottom);
        }
    }
}
